<template>
  <div>
    <!--条件筛选-->
    <TopFilter ref="topFilter" :list="topFilterList" :cols="3" labelWidth="110" @filterChange="changeHandle" @onCollapse="collapseHandle"></TopFilter>
    <!-- table -->
    <FilterTable
      ref="table"
      :columns="columns"
      :fetchapi="fetchApi"
      :params="fetchParams"
      :datakey="'records'"
      :isSelectColumn="false"
      :onColumnsChange="columns => this.columns = columns"
      :cellClick="cellClick"
      :cellstyles="cellstyles"
      :onSyncTableData="tableChangeFun"
      :isExportExcel2="true"
      :exportFileName="'服务商账户信息.xlsx'"
    >
    <!-- 导入导出暂定 -->
      <!-- :isExportExcel2="true"
      :exportFileName="'经销商资金账户.xlsx'" -->
      <template slot="controls" slot-scope="props">
        <el-button icon="el-icon-download" size="small" type="primary" :loading="downloading" @click="detailExportClick">明细导出</el-button>
      </template>
    </FilterTable>

    <BaseDialog
      :visible.sync="issearch"
      :title="'服务商查询'"
      destroyOnClose
      :containerStyle="{height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px'}"
    >
      <ownerSeacher  @close="dealercloseHandler" />
    </BaseDialog>

    <Drawer
      :visible.sync="openCashFlow"
      :title="'服务商资金异动明细'"
      destroyOnClose
      :containerStyle="{height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px'}"
    >
      <cashFlow :params="cashFlowParams" @close="cashFlowCloseHandler" />
    </Drawer>

    <Drawer
      :visible.sync="openFreezeDetails"
      :title="'冻结明细'"
      destroyOnClose
      :containerStyle="{height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px'}"
    >
      <freezeDetails  :params="freezeDetailParams" @close="cashFlowCloseHandler" />
    </Drawer>
  </div>
</template>

<script>
import { dictionary } from '@/utils/dictMixin';
import {getAccount,detailExport} from '@/api/partsmanage/financialManagement/capitalAccount/index';
import ownerSeacher from '@/pages/partsmanage/claim/approve/ownerSeacher';
import { notifyAction } from '@/utils';
import cashFlow from '@/pages/partsmanage/financialManagement/capitalAccount/customPopup/cashFlow';
import freezeDetails from '@/pages/partsmanage/financialManagement/capitalAccount/customPopup/freezeDetails';
import { exportFile } from '@/utils/index';
export default {
  name: "capitalAccount",
  mixins: [dictionary],
  components: {ownerSeacher,cashFlow,freezeDetails},
  data() {
    return{
      topFilterList: this.createTopFilterList(),
      columns: this.createTableColumns(),
      fetchApi: getAccount,
      issearch: false,
      openCashFlow: false,
      openFreezeDetails: false,
      cashFlowParams:{},
      freezeDetailParams:{},
      fetchParams: {xhrAbort: true},
      downloading: false,
      cellstyles:[]
    };
  },
  async created() {
  },
  methods: {
    /**
    * 账户资金明细导出
    */
    detailExportClick(){
      this.downloading = true;
      detailExport(this.fetchParams).then(res => {
        exportFile(res.data, '服务商账户明细.xls');
        this.downloading = false;
      }).catch(res =>{
        this.downloading = false;
      });
    },
    importExcel(){
      return notifyAction('功能开发中!', 'warning');
    },
    tableChangeFun(dataList) {
      dataList.forEach((item, index) => {
        // this.cellstyles.push({ row: item, dataIndex: 'accountCapital', styles: { backgroundColor: 'orange' } });
        // this.cellstyles.push({ row: item, dataIndex: 'frozenCapital', styles: { backgroundColor: 'yellow' } });
        this.cellstyles.push({ row: item, dataIndex: 'accountCapital', styles: { color: '#409EFF' } });
        this.cellstyles.push({ row: item, dataIndex: 'frozenCapital', styles: { color: '#409EFF' } });
      });
    },
    cashFlowCloseHandler(){
      this.openCashFlow = false;
      this.openFreezeDetails = false;
      // 重新加载
      this.fetchParams = Object.assign({}, this.fetchParams);
    },
    //当用户点击账户资金和冻结资金时弹框看明细
    cellClick(row,cellName,column,cell,event){
      if(cellName == "accountCapital"){
        this.openCashFlow = true;
        this.cashFlowParams = row;
      }else if(cellName == "frozenCapital"){
        this.openFreezeDetails = true;
        this.freezeDetailParams = row;
      }
    },
    createTopFilterList() {
      return[
        {
          type: 'INPUT',
          label: '服务商代码',
          fieldName: 'dealerCode',
          placeholder: '请选择',
          unitRender: () => {
            return (
              <div>
                <el-button icon="el-icon-search" onClick={() => this.seacherFun()}></el-button>
              </div>
            );
          }
        },
        {
          type: 'INPUT',
          label: '服务商简称',
          fieldName: 'dealerName',
          placeholder: '请输入',
        }
      ]
    },
    createTableColumns () {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 70,
          sorter: true,
          align:"center",
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '服务商代码',
          dataIndex: 'dealerCode',
          sorter: true,
          align:"center"
        },
        {
          title: '服务商简称',
          dataIndex: 'dealerName',
          sorter: true,
          align:"center"
        },
        {
          title: '账户资金',
          dataIndex: 'accountCapital',
          sorter: true,
          precision: 2,
          align:"center",
          numberFormat: true
        },
        {
          title: '冻结资金',
          dataIndex: 'frozenCapital',
          sorter: true,
          precision: 2,
          align:"center",
          numberFormat: true
        },
        {
          title: '可用余额',
          dataIndex: 'availableCapital',
          sorter: true,
          precision: 2,
          align:"center",
          numberFormat: true
        }
      ]
    },
    seacherFun(){
      this.issearch = true
    },
    dealercloseHandler(val){
      console.info(val)
      if(val){
        this.topFilterList.find(x=>x.fieldName=='dealerCode').initialValue=val.dealerCode;
        this.topFilterList.find(x=>x.fieldName=='dealerName').initialValue=val.dealerShortname;
      }
      this.issearch=false;
    },
    changeHandle(val) {
      this.fetchParams = Object.assign({}, val);
    },
    collapseHandle() {
      this.$nextTick(() => {
        this.BaseTable.EXECUTE_RESET_HEIGHT();
      });
    },
  }
}
</script>

<style lang="less" scoped>
.red {
  color: red !important;
}

.orange {
  color: orange;
}

.yellow {
  color: yellow;
}

/deep/.table-wrapper .el-table__body tbody>tr>td .cell{color: inherit !important;}
</style>
